

const { Op } = require("sequelize");

const { queryUtil, parseUtil } = require('../utils/query.util')

const Employee = require('../model/employee.model')
const Project = require('../model/project.model')
const ProjectEmp = require('../model/projectEmp.model')
class ProjectService {
    async createProject(project) {
        const { empIds } = project
        const res = await Project.create(project)
        const projectId = res.getDataValue('id')
        console.log(empIds);
        empIds.map(async (empId) => {
            await ProjectEmp.create({ empId, projectId })
        })
        return res ? res.dataValues : null
    }

    async updateById(project) {
        const { empIds, id, ...newProject } = project
        const res = await Project.update(newProject, { where: { id } })
        const projectId = id
        await ProjectEmp.destroy({ where: { projectId } })
        empIds.map(async (empId) => {
            await ProjectEmp.create({ empId, projectId })
        })
        return res[0] > 0 ? true : false
    }

    async updateStatusById(id, status) {
        let res
        if (status == 1) {
            console.log(status);
            const startTime = Date.now()
            res = Project.update({ status, startTime }, { where: { id } })

        } else {
            console.log(status);
            const endTime = Date.now()
            res = Project.update({ status, endTime }, { where: { id } })
        }
        return res[0] > 0 ? true : false
    }

    async queryAll(pageNum, pageSize, whereOpt) {
        let res = await queryUtil(pageNum, pageSize, whereOpt, Project)
        let list = res.list
        await parseUtil(list)

        // await Promise.all(list.map(
        //     async (project) => {
        //         project.dataValues.directorName = (await Employee.findOne({ attributes: ['empName'], where: { id: project.director } })).dataValues.empName
        //         const empIdList = await ProjectEmp.findAll({
        //             attributes: ['empId'],
        //             where: { projectId: project.dataValues.id }
        //         })
        //         let empIds = []

        //         empIdList.map((emp) => {
        //             empIds.push(emp.dataValues.empId)
        //         })
        //         const empNames = []
        //         const empNameList = await Employee.findAll({ attributes: ['empName'], where: { id: empIds } })

        //         empNameList.map((emp) => {
        //             empNames.push(emp.dataValues.empName)
        //         })
        //         // console.log(empNames);
        //         let employee = []
        //         for (let i = 0; i < empIds.length; i++) {
        //             employee.push({ 'id': empIds[i], 'empName': empNames[i] });
        //         }

        //         project.dataValues.employee = employee
        //     }
        // ))
        return res
    }

    async queryMyAll({empId,status}) {

        const res = await ProjectEmp.findAll({ where: {empId} })
        if(res){
            let projectId = []
            res.map((projectEmp) => {
                projectId.push(projectEmp.getDataValue('projectId'))
            })
            const whereOpt={id:projectId}
            if(status){
                // console.log(status);
                if(status==1) {
                    console.log(status);
                    whereOpt['status']={[Op.lte]: status }
                }else{
                    whereOpt['status']=status
                }
            }
            console.log(whereOpt);
            const data = await Project.findAll({ where: whereOpt })
            await parseUtil(data)
            return data
        } 
        return null
    }
}

module.exports = new ProjectService()